import {
	httpPost,
	httpGet,
	httpPut,
	httpDelete,
	httpFile
} from '../request.js'


/**
 * @title 登录
 * @param {*} identity 身份 1牛人 2企业
 */
export const login = (identity, nickname,
	header) => {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: async function({
				code
			}) {
				// 获取openid
				const {
					data: {
						openid
					}
				} = await httpGet('/wxapp/auth', {
					code
				})
				const {
					code: status,
					data
				} = await httpPost('/login', {
					openid: openid,
					identity,
					nickname,
					header
				})

				if (status == 200) {

					// 登录成功
					// 设置登录信息
					uni.setStorageSync('loginInfo', data)
					// 设置token
					uni.setStorageSync('token', data.token)
					await getUser()
					resolve(data)
				} else {
					reject(null)
				}
			}
		})
	})

}

/**
 * @title 获取用户信息
 */
export const getUser = async () => {
	const {
		data,
		code
	} = await httpGet('/user')
	if (code === 200) {
		// 设置用户信息
		uni.setStorageSync('userInfo', data)
		return data
	}
}

/**
 * @title 退出登录
 */
export const logout = () => httpPost('/logout')

/**
 * @title 用户反馈
 */
export const feedback = data => httpPost('/message', data)

/**
 * @title 修改用户基本信息
 */
export const editUser = data => httpPut('/user', data)

/**
 * @title 绑定手机号验证码
 */
export const userAuthSms = mobile => httpPost('/userAuthSms', {
	mobile
})


/**
 * @title 我的投递记录
 */
export const getDelivery = data => httpGet('/delivery', {
	page: data.page,
	size: data.size || 10,
	status: data.status
})


/**
 * @title 修改个人简历信息
 */
export const usercv = data => httpPut('/userbase', data)

/**
 * 上传简历
 * @param {} data 
 */
export const uploadUsercv = cv_url => httpPut('/usercv', {
	cv_url
})


/**
 * @title 添加工作经历
 */
export const userjob = data => httpPost('/userjob', data)

/**
 * @title 修改工作经历
 */
export const editUserjob = (data, id) => {
	console.log(data, id)
	return httpPut(`/userjob/${id}`, data)
}

/**
 * @title 获取工作经历详情
 */
export const getUserjob = id => httpGet(`/userjob/${id}`)

/**
 * @title 删除工作经历
 */
export const deleteUserjob = id => httpDelete(`/userjob/${id}`)


/**
 * @title 新增项目经历
 */

export const userproject = data => httpPost('/userproject', data)

/**
 * @title 获取项目经历详情
 */
export const getUserproject = id => httpGet(`/userproject/${id}`)

/**
 * @title 修改项目经历
 */
export const editUserproject = (data, id) => httpPut(`/userproject/${id}`, data)


/**
 * @title 删除项目经历
 */
export const deleteUserProject = id => httpDelete(`/userproject/${id}`)


/**
 * 新增教育经历
 */
export const usereducate = data => httpPost('/usereducate', data)

/**
 * @title 获取教育经历详情
 */
export const getUsereducate = id => httpGet(`/usereducate/${id}`)

/**
 * @title 修改教育经历详情
 * 
 */
export const editUsereducate = (data, id) => httpPut(`/usereducate/${id}`, data)

/**
 * @title 删除教育经历
 */
export const deleteUserDucate = id => httpDelete(`/usereducate/${id}`)

/**
 * @title 修改用户语言
 */
export const userlang = lang_id => httpPut('/userlang', {
	lang_id
})

/**
 * @title 修改个人技能
 */
export const usertag = tag_id => httpPut('/usertag', {
	tag_id
})

/**
 * @title 上传头像
 */
export const uploadAvatar = file => httpFile('/file', file)


/**
 * @title 用户身份绑定
 */
export const useridcard = data => httpPost('/useridcard', data)


/**
 * @title updateMobile
 */
export const updateMobile = data => httpPost('/updateMobile', data)


/**
 * @title 发送
 */
export const sendMobile = mobile => httpGet(`/updateMobile?mobile=${mobile}`)